package db

import (
	"database/sql"
	"encoding/json"
	"fmt"
	_ "github.com/mattn/go-sqlite3"
	"local_server/utils"
)

type MediaInfo struct {
	Name          string `json:"name"`
	Type          int    `json:"type"`
	TypeName      string `json:"type_name"`
	Path          string `json:"path"`
	PreName       string `json:"pre_name"`
	Image         string `json:"image"`
	Actor         string `json:"actor"`
	Desc          string `json:"desc"`
	Director      string `json:"director"`
	DatePublished string `json:"date_published"`
	Title         string `json:"title"`
	Zone          string `json:"zone"`
	Recommend     int    `json:"recommend"`
	Language      string `json:"language"`
}

func openDb() *sql.DB {
	path := utils.GetDbPath()
	println(path)
	db, e := sql.Open("sqlite3", path)
	utils.CheckErr(e)
	return db
}

func getQueryMediaList(rows *sql.Rows) string {
	jsonStr := "["
	for rows.Next() {
		println("---------")
		var info = &MediaInfo{}
		e := rows.Scan(&info.Name, &info.Type, &info.TypeName, &info.Path, &info.PreName, &info.Image, &info.Actor, &info.Desc, &info.Director, &info.DatePublished, &info.Title, &info.Zone, &info.Recommend, &info.Language)
		utils.CheckErr(e)

		s, e := json.Marshal(info)
		utils.CheckErr(e)
		jsonStr = jsonStr + string(s) + ","
	}
	if jsonStr == "[" {
		return "[]"
	}
	return jsonStr[0:len(jsonStr)-1] + "]"
}

func getAllColumn() string {
	colums := " IFNULL(name ,''),"
	colums += "IFNULL(type ,0),"
	colums += "IFNULL(type_name ,''),"
	colums += "IFNULL(path ,''),"
	colums += "IFNULL(pre_name ,''),"
	colums += "IFNULL(image ,''),"
	colums += "IFNULL(actor ,''),"
	colums += "IFNULL(desc ,''),"
	colums += "IFNULL(director ,''),"
	colums += "IFNULL(date_published ,''),"
	colums += "IFNULL(title ,''),"
	colums += "IFNULL(zone ,''),"
	colums += "IFNULL(recommend ,''),"
	colums += "IFNULL(language ,'') "
	return colums
}

func GetRecommendList() []string {
	db := openDb()
	defer db.Close()

	filmList := GetRecommend(1, 6, db)
	tv_list := GetRecommend(2, 6, db)
	anim_list := GetRecommend(3, 6, db)
	sing_list := GetRecommend(4, 6, db)
	record_list := GetRecommend(5, 6, db)
	println("tv")
	println(tv_list)
	return []string{filmList, tv_list, anim_list, sing_list, record_list}
}

func GetList(t int, start int, len int) string {
	db := openDb()
	defer db.Close()

	sqlStr := fmt.Sprintf("SELECT %s FROM media_list where recommend>=0 and type=%d LIMIT %d,%d", getAllColumn(), t, start, len)
	rows, e := db.Query(sqlStr)
	utils.CheckErr(e)
	return getQueryMediaList(rows)
}

func GetCount(t int) int {
	db := openDb()
	defer db.Close()

	sqlStr := fmt.Sprintf("SELECT count(*) FROM media_list where recommend>=0 and type=%d", t)
	rows, e := db.Query(sqlStr)
	utils.CheckErr(e)
	var c = 0
	if rows.Next() {
		_ = rows.Scan(&c)
	}
	return c
}

func GetRecommend(t int, count int, db *sql.DB) string {
	sqlStr := fmt.Sprintf("SELECT %s FROM media_list where recommend>=0 and type=%d ORDER BY RANDOM() LIMIT %d", getAllColumn(), t, count)
	rows, e := db.Query(sqlStr)
	utils.CheckErr(e)
	return getQueryMediaList(rows)
}

